<?php
session_start();
require_once("../models/Unit.php");
require_once("../models/Admin.php");
class LoginController{

	var $loginDetails;

	
	function sendLoginData(){
		$this->loginDetails['password'] = $_REQUEST['password'];
		$this->loginDetails['username'] = $_REQUEST['username'];
	}
	function isValidLoginInput(){
		if($this->loginDetails['username']==""||$this->loginDetails['password']==""){
			return false;
		}
		else return true;
	}
	
	
}

	$a = new LoginController;
	$a->sendLoginData();
	$unit = new Unit;
	$unit->connect();
	$admin = new Admin;
	$admin->connect();
	if(!$a->isValidLoginInput()){
		?>
			<span class="style3">Invalid input!</span>
		<?php
		die;
	}
	if($unit->isValidUnitLogin($a->loginDetails)){
		$_SESSION['username']=strtoupper($_REQUEST['username']);
		echo "Logged in Successfully!<br />";
		echo "<script type=\"text/javascript\">
			<!--
			function delayer(){
				window.location = \"index.php\";
			}
			setTimeout('delayer()', 1000);
			//-->
			</script>";
		echo "You wll be redirected within a second.";
		die;
	}
	else{
		if($admin->isValidAdminLogin($a->loginDetails)){
			$_SESSION['username']=$_REQUEST['username'];
			?>
			Logged in Successfully as Administrator!<br />
			<script type="text/javascript">
				<!--
				function delayer(){
					window.location = "index.php";
				}
				$("#loginform").html("");
				setTimeout('delayer()', 1000);
				//-->
			</script>
			You wll be redirected within a second.
			<?php
			die;
		}
		else echo '<span class="style3">Incorrect username or password</span>';
	}

	
?>